package com.umeox.watch.moto.launcher.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.event.WatchFaceChangeEvent;
import com.umeox.watch.moto.launcher.widget.AnalogClock;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class AnalogClockView extends RelativeLayout {


    private AnalogClock mAnalogClock;
    private Context mContext;
    private CharSequence lastDateTime;


    public AnalogClockView(Context context) {
        this(context, null);
    }

    public AnalogClockView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnalogClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        final View view = LayoutInflater.from(context).inflate(R.layout.view_clock_analog, this);
        mAnalogClock = view.findViewById(R.id.clock_analog);

        final int wallpaper = DataProvider.getWallpaper();
        initWallpaper(wallpaper);

    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onWatchFaceChangeEvent(WatchFaceChangeEvent event) {
        final int position = event.mPosition;
        initWallpaper(position);

    }



    private void initWallpaper(int wallpaper) {
        final String[] handHour = mContext.getResources().getStringArray(R.array.hand_hour);
        final String[] handMinute = mContext.getResources().getStringArray(R.array.hand_minute);
        final String[] handSecond = mContext.getResources().getStringArray(R.array.hand_second);
        final int[] handSecondNailOffset = mContext.getResources().getIntArray(R.array.hand_second_nail_offset);
        final int[] handHourMinNailOffset = mContext.getResources().getIntArray(R.array.hand_hour_min_nail_offset);
        mAnalogClock.setHandHourBitmap(getBitmap(handHour[wallpaper]));
        mAnalogClock.setHandMinutesBitmap(getBitmap(handMinute[wallpaper]));
        mAnalogClock.setHandSecondsBitmap(getBitmap(handSecond[wallpaper]));
        mAnalogClock.setSecondHandNailOffset(handSecondNailOffset[wallpaper]);
        mAnalogClock.setHandNail(handHourMinNailOffset[wallpaper]);
    }

    private Bitmap getBitmap(String name) {
        final int id = getResources().getIdentifier(name, "drawable", mContext.getPackageName());
        return BitmapFactory.decodeResource(mContext.getResources(),id);
    }

}
